package com.chennan.rebbitmq.nantest.simple;

import com.chennan.rebbitmq.constant.RabbitMQExchangeEnum;
import com.chennan.rebbitmq.uitl.MqConnection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Consumer;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author chennan
 * @create 2022-04
 */
public class ConsumerSimple {

    public final static String ConsumerSimpleExchange = "ConsumerSimpleExchange";
    public final static String ConsumerSimpleQueue = "ConsumerSimpleQueue";
    public final static String SimpleroutingKey = "SimpleroutingKey";



    public static void main(String[] args) throws Exception {

        new Thread(()->{

            try {

                new ConsumerSimple().executeConsumer();

                System.in.read();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }).start();
    }

    public void executeConsumer() throws Exception {
        Connection connection = MqConnection.getConnection();
        Channel channel = connection.createChannel();
        //1、创建交换机
        MqConnection.exchangeDeclare(channel,ConsumerSimpleExchange, RabbitMQExchangeEnum.DIRECT);
        //2、声明队列
        MqConnection.queueDeclare(channel,ConsumerSimpleQueue);
        //3、绑定交换机和队列
        MqConnection.queueBind(channel,ConsumerSimpleExchange,ConsumerSimpleQueue,SimpleroutingKey);
        //4、创建消费者
        Consumer defaultConsumer = MqConnection.createDefaultConsumer(channel);
        //5、监听消息
        MqConnection.basicConsume(channel,ConsumerSimpleQueue,true,defaultConsumer);
    }
}
